package com.bobi.tfca.ui.mainnavigation.home.rights

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModel
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.PageElementIdConstant
import com.bobi.tfca.databinding.FragmentMemberRightsBinding
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.ResourceAPI
import com.bobi.tfca.util.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.RequestBody


class MemberRightsFragment : BaseFragment<FragmentMemberRightsBinding, ViewModel>(
    FragmentMemberRightsBinding::inflate,
    null,
    true
) {
    // 软键盘首次打开或是首次关闭
    private var keyboardFlag = false


    @SuppressLint("ResourceType", "UseRequireInsteadOfGet", "ClickableViewAccessibility")
    override fun initFragment(
        binding: FragmentMemberRightsBinding,
        viewModel: ViewModel?,
        savedInstanceState: Bundle?
    ) {

        // 设置菜单为监听的
        publicViewModel!!.apply {
            visiblePageElements.observe(viewLifecycleOwner) {
                binding.ivEdit.visibility = getElementVisibility(PageElementIdConstant.FRAGMENT_MEMBER_RIGHTS_IV_EDIT)
            }
        }

        // 设置修改按钮监听
        binding.ivEdit.setOnClickListener {
            // 隐藏修改按钮，显示保存按钮
            binding.ivEdit.visibility = View.GONE
            binding.btSave.visibility = View.VISIBLE
            // 隐藏tv,显示et
            binding.svContent.visibility = View.GONE
            binding.svEditContent.visibility = View.VISIBLE
        }

        // 设置保存按钮监听
        binding.btSave.setOnClickListener {
            val change = binding.etContent.text.toString()
            LogUtil.i("change-------$change")
            saveRightsChange(change, binding)
        }

        getMemberRights(binding)
    }

    private fun saveRightsChange(change: String, binding: FragmentMemberRightsBinding) {
        val requestBody = RequestBody.create(MediaType.parse("text/plain"), change)
        publicViewModel!!.apply {
            request(ResourceAPI::class.java).saveRightsChange(requestBody).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                binding.tvMemberRights.text = change
                                binding.etContent.setText(change)
                                // 隐藏保存按钮，显示修改按钮
                                binding.btSave.visibility = View.GONE
                                binding.ivEdit.visibility = View.VISIBLE
                                // 显示tv,隐藏et
                                binding.svEditContent.visibility = View.GONE
                                binding.svContent.visibility = View.VISIBLE
                            }
                        }
                    }
                }
            }
        }
    }

    private fun getMemberRights(binding: FragmentMemberRightsBinding) {
        publicViewModel!!.apply {
            request(ResourceAPI::class.java).getMemberRights().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val result = it.data?.data
                                if (result != null) {
                                    binding.tvMemberRights.text = result
                                    binding.etContent.setText(result)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}